<template>
  <div class="dept">
    <section class="query-form">
      <el-form :inline="true" ref="queryForm" :model="queryForm">
        <el-form-item>
          <el-input
            placeholder="请输入部门名称"
            v-model="queryForm.deptName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDeptList" type="primary">查询</el-button>
          <el-button @click="handleReset('queryForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </section>
    <section class="base-table">
      <section class="action">
        <el-button type="primary" @click="handleOpen">创建</el-button>
      </section>
      <el-table
        :data="deptList"
        row-key="_id"
        :tree-props="{ children: 'children' }"
      >
        <el-table-column
          v-for="column in columns"
          :key="column.prop"
          v-bind="column"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </section>
    <el-dialog title="创建部门" v-model="showModal">
      <el-form
        ref="dialogForm"
        :model="deptForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="上级部门" prop="parentId">
          <el-cascader
            placeholder="情选择上级部门"
            v-model="deptForm.parentId"
            :props="{ checkStrictly: true, value: '_id', label: 'deptName' }"
            :options="deptList"
            :show-all-levels="true"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input
            placeholder="请输入部门名称"
            v-model="deptForm.deptName"
          ></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="user">
          <el-select
            placeholder="请选择部门负责人"
            v-model="deptForm.user"
            @change="handleUser"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.userName"
              :value="`${item.userId}/${item.userName}/${item.userEmail}`"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人邮箱" prop="userEmail">
          <el-input
            placeholder="请输入负责人邮箱"
            v-model="deptForm.userEmail"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-foot">
          <el-button @click="handleClose">取消</el-button>
          <el-button @click="handleSubmit" type="primary">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Dept',
  data() {
    return {
      queryForm: { deptName: '' },
      columns: [
        {
          label: '部门名称',
          prop: 'deptName'
        },
        {
          label: '负责人',
          prop: 'userName'
        },
        {
          label: '更新时间',
          prop: 'updateTime'
        },
        {
          label: '创建时间',
          prop: 'createTime'
        }
      ],
      deptList: [],
      pager: {
        pageNum: 1,
        pageSize: 10
      },
      action: '',
      showModal: false,
      userList: [],
      deptForm: {
        parentId: [null]
      },
      rules: {
        parentId: [
          {
            required: true,
            message: '请选择上级部门'
          }
        ],
        deptName: [
          {
            required: true,
            message: '请输入部门名称'
          }
        ],
        user: [
          {
            required: true,
            message: '请输入负责人'
          }
        ]
      }
    }
  },
  mounted() {
    this.getDeptList()

    this.getAllUserList()
  },
  methods: {
    async getDeptList() {
      const list = await this.$api.getDeptList({
        ...this.queryForm
      })
      this.deptList = list
    },
    async getAllUserList() {
      const list = await this.$api.getAllUserList()
      this.userList = list
    },
    handleReset(form) {
      this.$refs[form].resetFields()
    },
    handleOpen() {
      this.action = 'create'
      this.showModal = true
    },
    handleEdit(row) {
      this.action = 'edit'
      this.showModal = true

      this.$nextTick(() => {
        Object.assign(this.deptForm, row, {
          user: `${row.userId}/${row.userName}/${row.userEmail}`
        })
      })
    },
    async handleDel(row) {
      this.action = 'delete'

      await this.$api.deptOperate({
        _id: row._id,
        action: this.action
      })

      this.$message.success('删除成功')
      this.getDeptList()
    },
    handleClose() {
      this.showModal = false
      this.handleReset('dialogForm')
    },
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          const params = { ...this.deptForm, action: this.action }
          delete params.user
          await this.$api.deptOperate(params)
          this.$message.success('操作成功')
          this.handleClose()
          this.getDeptList()
        }
      })
    },
    handleUser(val) {
      const [userId, userName, userEmail] = val.split('/')
      Object.assign(this.deptForm, { userId, userName, userEmail })
    }
  }
}
</script>

<style lang="scss" scoped></style>
